<template>
  <div class="divBox">
    <el-row class="ivu-mt box-wrapper acea-row">
      <div class="left-wrapper">
        <el-tabs :tab-position="tabPosition" v-model="cardShow">
          <el-tab-pane v-for="(item, index) in menuList" :key="index" :label="item.name" :name="index.toString()">用户管理</el-tab-pane>
        </el-tabs>
      </div>
      <div class="right-wrapper">
        <div>
          <div v-if="cardShow==1 || cardShow==2"
            class="product_tabs"
            :style="'padding-right:'+(menuCollapse?105:20)+'px'"
          >
            <div slot="title">
              <div>
                <el-button type="primary" size="small" @click="submit" :loading="loadingExist">保存</el-button>
                <el-button size="small" @click="reast">重置</el-button>
              </div>
            </div>
          </div>
        </div>
        <el-card v-if="cardShow==0">
          <div v-if="cardShow==0" class="acea-row">
            <div style="width: 350px;height:550px;margin-right: 20px;position: relative;" v-if="isDiy">
              <iframe id="iframe" class="iframe-box" :src="imgUrl" frameborder="0" ref="iframe"></iframe>
              <!-- <div class="mask"></div> -->
            </div>
            <div class="table">
              <div class="acea-row row-between-wrapper">
                <el-row type="flex">
                  <div>
                    <div class="acea-row row-between-wrapper">
                      <div class="button acea-row row-middle">
                        <el-button type="primary" size="small" @click="add" style="font-size: 12px;"><i class="el-icon-plus" style="margin-right: 4px;"/>添加</el-button>
                      </div>
                      <div style="color:#F56464;font-size: 13px;">&nbsp;&nbsp;注：初次进入该页面，可直接添加商城首页模板，也可先复制默认模板，再编辑默认模板保存后设为首页。</div>
                    </div>
                  </div>
                </el-row>
              </div>
              <el-table
                class="tables"
                :data="list"
                ref="table"
                highlight-current-row
                size="small"
                v-loading="loading"   
              >
                <el-table-column prop="id" label="页面ID" min-width="50" />
                <el-table-column prop="name" label="模板名称" min-width="100" />
                <el-table-column prop="add_time" label="添加时间" min-width="120" />
                <el-table-column prop="update_time" label="更新时间" min-width="120" />
                <el-table-column label="操作" min-width="180" fixed="right">
                  <template slot-scope="scope">
                    <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
                    <el-button type="text" size="small" @click="del(scope.row.id, scope.$index)">删除</el-button>
                    <el-button type="text" size="small" v-if="scope.row.status != 1" @click="setStatus(scope.row, scope.$index)">设为首页</el-button>
                    <el-button type="text" size="small" v-if="scope.row.is_diy" class="copy-data" @click="preview(scope.row)">预览</el-button>
                    <div style="display: inline-block" v-if="!scope.row.is_diy">
                      <el-button type="text" size="small" @click="recovery(scope.row, scope.$index)">恢复初始设置</el-button>
                    </div>
                    <el-button type="text" size="small" @click="onDiyCopy(scope.row)">复制</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div class="block">
                <el-pagination
                  background
                  :page-size="diyFrom.limit"
                  :current-page="diyFrom.page"
                  layout="total, prev, pager, next, jumper"
                  :total="total"
                  @size-change="handleSizeChange"
                  @current-change="pageChange"
                />
              </div>
            </div>
          </div>
        </el-card>
        <shopStreet v-else-if="cardShow==1" ref="shopStreet" @parentFun="getChildData"></shopStreet>
        <users v-else ref="users" @parentFun="getChildData"></users>
      </div>
    </el-row>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import SettingMer from '@/libs/settingMer'
import { roterPre } from '@/settings'
import { diyList, diyDel, setStatus, recovery, diyCopy } from "@/api/diy";
import { mapState } from "vuex";
import shopStreet from './shopStreet'
import users from './users'
export default {
  name: "devise_list",
  computed: {
    ...mapState('layout', [
      'menuCollapse'
    ])
  },
  components: {
    shopStreet,
    users
  },
  data() {
    return {
      grid: {
        sm: 10,
        md: 12,
        lg: 19,
      },
      loading: false,
      theme3: "light",
      roterPre: roterPre,
      menuList:[
        {
          name:'商城首页',
          id:1
        },
        {
          name:'店铺街',
          id:2
        },
        {
          name:'个人中心',
          id:3
        },
      ],
      list: [],
      imgUrl:'',
      tabPosition: "left",
      modal: false,
      BaseURL: SettingMer.httpUrl || 'http://localhost:8080',
      cardShow: 0,
      loadingExist: false,
      isDiy: 1,
      qrcodeImg: '',
      diyFrom: {
        page: 1,
        limit: 10
      },
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  mounted: function() {
    this.$store.commit("settings/STOREDIY", 0);
  },
  methods: {
    getChildData(e){
      this.loadingExist = e
    },
    submit(){
      if(this.cardShow==1){
        this.$refs.shopStreet.onSubmit()
      }else {
        this.$refs.users.onSubmit()
      }
    },
    reast(){
      if(this.cardShow==1){
        this.$refs.shopStreet.getInfo()
      }else {
        this.$refs.users.getInfo();
      }
    },
    bindMenuItem(index) {
      this.cardShow = index;
    },
    preview(row){
      let time = new Date().getTime() * 1000
      let imgUrl = `${this.BaseURL}/pages/index/index?inner_frame=1&diyId=${row.id}&time=${time}`;
      this.imgUrl = imgUrl;
    },
    // 获取列表
    getList() {
      let storage = window.localStorage;
      this.imgUrl = storage.getItem('imgUrl');
      let that = this
      this.loading = true;
      diyList(this.diyFrom).then((res) => {
        this.loading = false;
        let data = res.data;
        this.list = data.list;
        this.total = data.count;
        let time = new Date().getTime() * 1000
        let imgUrl = `${that.BaseURL}/pages/index/index?inner_frame=1&time=${time}`;
        storage.setItem('imgUrl',imgUrl)
        that.imgUrl = imgUrl;
      }).catch(({ message }) => {
        this.loading = false;
        this.$message.error(message)
      });
    },
    pageChange(status) {
      this.diyFrom.page = status;
      this.getList();
    },
    handleSizeChange(val) {
      this.diyFrom.limit = val
      this.getList()
    },
    // 编辑
    edit(row) {
      this.$router.push({
        path: `${roterPre}/setting/diy/index`,
        query: { id: row.id, name: row.template_name || "moren", types: 1 },
      });
    },
    // 添加
    add() {
      this.$router.push({
        path: `${roterPre}/setting/diy/index`,
        query: { id: 0, name: "首页", types: 1 },
      });
    },
    // 删除
    del(id,idx) {
      this.$modalSure('删除模板吗').then(() => {
        diyDel(id).then(({ message }) => {
          this.$message.success(message)
          this.getList()
        }).catch(({ message }) => {
          this.$message.error(message)
        })
      })
    },
    // 使用模板
    async setStatus(row) {
      let that = this
      that.$modalSure("把该模板设为首页").then(() => {
        setStatus(row.id).then((res) => {
            that.$message.success(res.message);
            that.getList();
          }).catch((res) => {
            that.$message.error(res.message);
          });
        })
    },
    recovery(row) {
      recovery(row.id).then((res) => {
        this.$message.success(res.message);
        this.getList();
      });
    },
    onDiyCopy(row) {
      diyCopy(row.id).then(() => {
        this.getList()
      }).catch(res => {
        this.$message.error(res.message);
      })
    }
  },
};
</script>

<style scoped lang="scss">
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color: #69A8EA !important;
  }
  ::v-deep .spike-bd .spike-distance .bg-red{
    width: 45px;
  }
  .product_tabs{
    padding: 15px 32px 0;
    background: #fff;
    text-align: right;
  }
  .el-menu-item{
    height: 47px;
  }
  .el-menu-item.is-active::after{
    content: "";
    display: block;
    width: 2px;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    background: var(--prev-color-primary)!important;
  }
  .tables{
    margin-top: 20px;
  }
  .ivu-mt{
    background-color: #fff;
    padding-bottom: 50px;
  }
  .bnt{
    width: 80px!important;
  }
  .iframe-box{
    width: 350px;
    height: 100%;
    border-radius: 10px;
    border: 1px solid #eee;
  }
  .mask{
    position: absolute;
    left:0;
    width: 100%;
    top:0;
    height: 100%;
    background-color: rgba(0,0,0,0);
  }
  .table{
    width: calc(100% - 390px);
  }
  .right-wrapper{
    width: calc(100% - 100px);
  }
  .code{
    position: relative;
  }
  .QRpic {
    width: 160px;
    height: 160px;

    img {
      width: 100%;
      height: 100%;
    }
  }
  .left-wrapper {
    width: 100px;
    background: #fff;
    border-right: 1px solid #dcdee2;
  }
  .picCon{
    width: 280px;
    height: 510px;
    background: #FFFFFF;
    border: 1px solid #EEEEEE;
    border-radius: 25px;
    .pictrue{
      width: 250px;
      height: 417px;
      border: 1px solid #EEEEEE;
      opacity: 1;
      border-radius: 10px;
      margin: 30px auto 0 auto;
      img{
        width: 100%;
        height: 100%;
        border-radius: 10px;
      }
    }
    .circle{
      width: 36px;
      height: 36px;
      background: #FFFFFF;
      border: 1px solid #EEEEEE;
      border-radius: 50%;
      margin: 13px auto 0 auto;
    }
  }
</style>
